/**
 * https://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/api/shop.js
 * Mocking client-server processing
 */
const axios = require('axios');

import userManager from '../util/UserManager';

//const BASE_URL = "http://localhost:9000";
const BASE_URL = "http://47.101.53.90:80";

export default {

    /**
     * 登录
     * @param userName
     * @param password
     * @param callback
     */
    requestLogin(userName, password, callback) {
        this._doRequest("POST", '/api/login', {
            userName: userName,
            password: password
        }, null, callback);
    },

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @param callback
     */
    requestModifyPassword(password, newPassword, callback) {
        this._doRequest("POST", '/api/modifyPassword', {
            password: password,
            newPassword: newPassword
        }, null, callback);
    },

    /**
     * 获取banner列表
     * @param type
     * @param callback
     */
    requestBannerAll(type, callback) {
        this._doRequest("POST", '/api/getBannerAll', {
            type: type
        }, null, callback);
    },

    /**
     * 获取banner
     * @param id
     * @param callback
     */
    requestBanner(id, callback) {
        this._doRequest("POST", '/api/getBanner', {
            id: id
        }, null, callback);
    },


    /**
     * 添加banner
     * @param banner
     * @param callback
     */
    requestAddBanner(banner, callback) {
        this._doRequest("POST", '/api/addBanner', {
            type: banner["type"],
            imageUrl: banner["imageUrl"],
            title: banner["title"],
            isNew: banner["isNew"],
            isCheckCredit: banner["isCheckCredit"],
            isAutoApproval: banner["isAutoApproval"],
            isMaxLowFee: banner["isMaxLowFee"],
            max: banner["max"],
            rate: banner["rate"],
            desp: banner["desp"],
            rank: banner["rank"],
            webUrl: banner["webUrl"],
        }, null, callback);
    },

    /**
     * 更新banner
     * @param banner
     * @param callback
     */
    requestUpdateBanner(banner, callback) {
        this._doRequest("POST", '/api/updateBanner', {
            id: banner["id"],
            type: banner["type"],
            imageUrl: banner["imageUrl"],
            title: banner["title"],
            isNew: banner["isNew"],
            isCheckCredit: banner["isCheckCredit"],
            isAutoApproval: banner["isAutoApproval"],
            isMaxLowFee: banner["isMaxLowFee"],
            max: banner["max"],
            rate: banner["rate"],
            desp: banner["desp"],
            rank: banner["rank"],
            webUrl: banner["webUrl"],

          /*  isAutoApproval: 0,
            isNew: 0,
            isMaxLowFee: 0,
            isCheckCredit: 0,*/
        }, null, callback);
    },

    /**
     * 删除banner
     * @param banner
     * @param callback
     */
    requestDeleteBanner(id, callback) {
        this._doRequest("POST", '/api/deleteBanner', {
            id: id
        }, null, callback);
    },


    _doRequest(method, url, params, data, callback) {
        let result = {
            code: 0,
            message: "成功",
            data: null,
            isSuccess: function () {
                return this.code === 0;
            }
        };

        let axiosParam = null;
        let headers = {};

        if (userManager.isLogin()) {
            headers["AccessToken"] = userManager.getUser()["accessToken"];
        }

        if (!!params) {
            headers["Content-Type"] = "application/x-www-form-urlencoded";

            axiosParam = {
                method: method,
                baseURL: BASE_URL,
                url: url,
                //headers: {'content-type': 'application/x-www-form-urlencoded'},
                headers: headers,
                params: params
            };
        }

        if (!!data) {
            axiosParam = {
                method: method,
                baseURL: BASE_URL,
                url: url,
                //headers: {'content-type': 'application/json'},
                headers: headers,
                data: data
            }
        }

        let doCallback = function (result) {
            console.log("HTTP-RESPONSE:" + JSON.stringify(result));
            callback(result);
        };

        console.log("HTTP-REQUEST:" + JSON.stringify(axiosParam));

        axios(axiosParam).then(function (response) {
            console.log(response);
            if (response.status === 200) {
                result.code = response.data.code;
                result.message = response.data.message;
                result.data = response.data.data;
                doCallback(result);
            } else {
                result.code = -1;
                result.message = "请求失败";
                doCallback(result);
            }
        }).catch(function (error) {
            console.log(error);
            result.code = -2;
            result.message = "网络错误";
            doCallback(result);
        });
    }
}
